package org.jeecg.modules.health.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.health.admin.entity.Healthstate;
import org.jeecg.modules.health.admin.service.IHealthstateService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static jdk.nashorn.internal.parser.DateParser.DAY;
import static jdk.nashorn.internal.runtime.regexp.joni.constants.TokenType.INTERVAL;

/**
 * @Description: 健康状态表
 * @Author: jeecg-boot
 * @Date:   2020-04-10
 * @Version: V1.0
 */
@Slf4j
@Api(tags="健康状态表")
@RestController
@RequestMapping("/admin/healthstate")
public class HealthstateController extends JeecgController<Healthstate, IHealthstateService> {
	@Autowired
	private IHealthstateService healthstateService;
	/**
	 * 分页列表查询
	 *
	 * @param healthstate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "健康状态表-分页列表查询")
	@ApiOperation(value="健康状态表-分页列表查询", notes="健康状态表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Healthstate healthstate,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Healthstate> queryWrapper = QueryGenerator.initQueryWrapper(healthstate, req.getParameterMap());
		Page<Healthstate> page = new Page<Healthstate>(pageNo, pageSize);
		IPage<Healthstate> pageList = healthstateService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param healthstate
	 * @return
	 */
	@AutoLog(value = "健康状态表-添加")
	@ApiOperation(value="健康状态表-添加", notes="健康状态表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Healthstate healthstate) {
		healthstateService.save(healthstate);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param healthstate
	 * @return
	 */
	@AutoLog(value = "健康状态表-编辑")
	@ApiOperation(value="健康状态表-编辑", notes="健康状态表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Healthstate healthstate) {
		healthstateService.updateById(healthstate);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "健康状态表-通过id删除")
	@ApiOperation(value="健康状态表-通过id删除", notes="健康状态表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		healthstateService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "健康状态表-批量删除")
	@ApiOperation(value="健康状态表-批量删除", notes="健康状态表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.healthstateService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "健康状态表-通过id查询")
	@ApiOperation(value="健康状态表-通过id查询", notes="健康状态表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Healthstate healthstate = healthstateService.getById(id);
		return Result.ok(healthstate);
	}

  /**
   * 导出excel 心率
   *
   * @param request
   * @param healthstate
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, @RequestBody Healthstate healthstate) {
	  QueryWrapper<Healthstate> queryWrapper = QueryGenerator.initQueryWrapper(healthstate, request.getParameterMap());
	  queryWrapper.isNotNull("heartbeat");
      return super.exportXls(request, healthstate, Healthstate.class, "健康状态表",queryWrapper);
  }
	/**
	 * 导出excel 体温
	 *
	 * @param request
	 * @param healthstate
	 */
	@RequestMapping(value = "/exportXls1")
	public ModelAndView exportXls1(HttpServletRequest request, @RequestBody Healthstate healthstate) {
		QueryWrapper<Healthstate> queryWrapper = QueryGenerator.initQueryWrapper(healthstate, request.getParameterMap());
		queryWrapper.isNotNull("animalheat");
		return super.exportXls(request, healthstate, Healthstate.class, "健康状态表",queryWrapper);
	}
	/**
	 * 导出excel 血压
	 *
	 * @param request
	 * @param healthstate
	 */
	@RequestMapping(value = "/exportXls2")
	public ModelAndView exportXls2(HttpServletRequest request, @RequestBody Healthstate healthstate) {
		QueryWrapper<Healthstate> queryWrapper = QueryGenerator.initQueryWrapper(healthstate, request.getParameterMap());
		queryWrapper.isNotNull("pressurelow").isNotNull("pressurehigh");
		return super.exportXls(request, healthstate, Healthstate.class, "健康状态表",queryWrapper);
	}

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Healthstate.class);
  }

	 /**
	  * 通过时间查询心率
	  *
	  * @param healthstate
	  * @return
	  */
	 @AutoLog(value = "健康状态表-通过create_time查询")
	 @ApiOperation(value="健康状态表-通过create_time查询", notes="健康状态表-通过create_time查询")
	 @PostMapping(value = "/queryByDate")
	 public Result<?> queryByDate(@RequestBody Healthstate healthstate) throws ParseException {
		 QueryWrapper queryWrapper = new QueryWrapper<Healthstate>()
				 .select("heartbeat","create_time")
				 .eq("user_account",healthstate.getUserAccount())
				 .apply("create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)")
				 .isNotNull("heartbeat");
		 List list= healthstateService.list(queryWrapper);
		 return Result.ok(list);
	 }

	/**
	 * 通过时间查询体温
	 *
	 * @param healthstate
	 * @return
	 */
	@AutoLog(value = "健康状态表-通过create_time查询")
	@ApiOperation(value="健康状态表-通过create_time查询", notes="健康状态表-通过create_time查询")
	@PostMapping(value = "/queryByDate1")
	public Result<?> queryByDate1(@RequestBody Healthstate healthstate) throws ParseException {
		QueryWrapper queryWrapper = new QueryWrapper<Healthstate>()
				.select("animalheat","create_time")
				.eq("user_account",healthstate.getUserAccount())
				.apply("create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)")
				.isNotNull("animalheat");
		List list= healthstateService.list(queryWrapper);
		return Result.ok(list);
	}

	/**
	 * 通过时间查询血压
	 *
	 * @param healthstate
	 * @return
	 */
	@AutoLog(value = "健康状态表-通过create_time查询")
	@ApiOperation(value="健康状态表-通过create_time查询", notes="健康状态表-通过create_time查询")
	@PostMapping(value = "/queryByDate2")
	public Result<?> queryByDate2(@RequestBody Healthstate healthstate) throws ParseException {
		QueryWrapper queryWrapper = new QueryWrapper<Healthstate>()
				.select("pressurelow","pressurehigh","create_time")
				.eq("user_account",healthstate.getUserAccount())
				.apply("create_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)")
				.isNotNull("pressurelow")
				.isNotNull("pressurehigh");
		List list= healthstateService.list(queryWrapper);
		return Result.ok(list);
	}


}
